package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Admin;
import entity.LayRequest;
import entity.Resource;
import entity.Role;
import mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service//service层注解
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public boolean insert(Role role) {
        return roleMapper.insert(role)>0?true:false;
    }

    @Override
    public List<Resource> selectByRoleId(Integer roleid) {
        return roleMapper.selectByRoleId(roleid);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public boolean delete(Integer id) {
        return roleMapper.delete(id)>0?true:false;
    }

    @Override
    public int update(Role role) {
        role.setUpdatetime(new Date());
        return roleMapper.update(role);
    }

    @Override
    public Role selectById(Integer id) {
        Role role=roleMapper.selectById(id);
        return role;
    }

    @Override
    public PageInfo<Role> selectByPage(LayRequest layRequest) {
        //1.设置当前页和每页记录条数
        PageHelper.startPage(layRequest.getPage(),layRequest.getLimit());
        //2.执行相关查询，自动转化为分页查询
        List<Role> roles=roleMapper.selectByRole(layRequest.getParams());
        //3.创建pageInfo对象，传入查询结果,会自动赋值总页数，总记录数，当前页等数据
        PageInfo<Role> pageInfo=new PageInfo<>(roles);
        return pageInfo;
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return roleMapper.batchDelete(ids);
    }
}
